//
//  HBInvitRecordCtrl.m
//  HuoBao
//
//  Created by 于会海 on 15/12/7.
//  Copyright © 2015年 jinxin. All rights reserved.
//

#import "HBInvitRecordCtrl.h"
#import "HBLoginManager.h"
#import "HBInvitRecordCell.h"
#import "HBListInviteUserInfo.h"
#import "HBListInviteUserParam.h"
#import "HBListInviteUserParser.h"
#import "HBListInviteUserItemInfo.h"

@interface HBInvitRecordCtrl ()<UITableViewDataSource,UITableViewDelegate>

{
    UITableView* invitTableView;
    UILabel*     userName;
    UILabel*     registDate;
    UILabel*     status;
    UILabel*     rewardLabel;
    NSArray*     dataArr;
}

#define LeftW  15.0;
#define GetFinalresult(a,b) (isIphone4or5?a:b)
@end

@implementation HBInvitRecordCtrl

- (void)viewDidLoad {
    [super viewDidLoad];    
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
}

- (void)buildLayout{
    [self cteateTopLabel];
    [self.view setBackgroundColor:[LCCommon getColor:@"37383d"]];
    
    invitTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 43, SCREEN_WIDTH, SCREEN_HEIGHT -64 -44 -43) style:UITableViewStylePlain];
    invitTableView.delegate = self;
    invitTableView.dataSource = self;
    [invitTableView setBackgroundColor:[UIColor clearColor]];
    [self.view addSubview:invitTableView];
    [invitTableView registerClass:[HBInvitRecordCell class] forCellReuseIdentifier:@"invitCell"];
    [invitTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [invitTableView setEditing:NO];
    [self requestListUser];
}

- (void)makeBackNomal{
    [self makeLinewithRect:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) Color:[LCCommon getColor:@"38393d"] addview:self.view];
    UIImage* image = [UIImage imageNamed:@"邀请码-缺省记录"];
    UIImageView* normalBack = [[UIImageView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT* 126/667, image.size.width, image.size.height)];
    normalBack.image = image;
    normalBack.centerX = self.view.centerX;
    [self.view addSubview:normalBack];
}

- (void)requestListUser{
    HBListInviteUserParam* param = [[HBListInviteUserParam alloc] init];
    param.uid = [HBLoginManager sharedHBLoginManager].currentUserInfo.uid;
    param.token = [HBLoginManager sharedHBLoginManager].currentUserInfo.token;
    HBListInviteUserParser* parser = [[HBListInviteUserParser alloc] init];
    [NetCenter getDataFromURL:Method_listInviteUser params:param parser:parser result:^(ParserResult *myResult) {
        if (myResult.code == 0) {
            HBListInviteUserInfo* info = (HBListInviteUserInfo*)myResult.result;
           // dataArr = [NSArray array];
            dataArr = [NSArray arrayWithArray:info.dataAry];
            if (dataArr.count == 0) {
                [self  makeBackNomal];
            }
            [invitTableView reloadData];
        }else{
            DISSMISS_ERR(myResult.message);
        }
        
    } failure:^(NSError *error) {
        DISSMISS_ERR(error.description);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataArr.count;
}

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString* identifier = @"invitCell";
    HBInvitRecordCell* cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    HBListInviteUserItemInfo* info = dataArr[indexPath.row];
        cell.userInteractionEnabled = NO;
    //cell.userNameX = userName.origin.x;
//        cell.registDateX = registDate.origin.x;
        cell.statusX = status.origin.x;
//        cell.rewardX = rewardLabel.origin.x;

        cell.phoneNumber = info.userMobile;
//        cell.registDate = info.reward;
        cell.status = info.status;
    
    [cell reloadCell];
    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 34;
}

- (void)cteateTopLabel{
    userName = [self LabelWithRect:CGRectMake(30, 13, 36, 16) textColor:[LCCommon getColor:@"888888"] textstr:@"用户名" textfont:12 textstyle:NSTextAlignmentLeft];
//    registDate = [self LabelWithRect:CGRectMake(userName.origin.x +36 +GetFinalresult(75, 85), userName.origin.y, GetFinalresult(110, 150), 16) textColor:[LCCommon getColor:@"888888"] textstr:@"奖励" textfont:12 textstyle:NSTextAlignmentLeft];
    status = [self LabelWithRect:CGRectMake(SCREEN_WIDTH -30 -26, userName.origin.y, 24, 16) textColor:[LCCommon getColor:@"888888"] textstr:@"状态" textfont:12 textstyle:NSTextAlignmentRight];
    [self makeLinewithRect:CGRectMake(15, 35, SCREEN_WIDTH -30, 1) Color:[LCCommon getColor:@"454545"] addview:self.view];
}


-(UILabel *)LabelWithRect:(CGRect)rect textColor:(UIColor *)color textstr:(NSString *)str textfont:(int)font textstyle:(NSTextAlignment)aligment{
    UILabel *label = [[UILabel alloc] initWithFrame:rect];
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc]initWithString:str];
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc]init];
    //设置行距
    [style setLineSpacing:10.0f];
    [attStr addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, [attStr length])];
    label.attributedText = attStr;
    label.textColor = color;
    label.font = [UIFont systemFontOfSize:font];
    label.textAlignment = aligment;
    label.numberOfLines = 0;
    [self.view addSubview:label];
    return label;
}

-(void)makeLinewithRect:(CGRect)rect Color:(UIColor *)color addview:(UIView *)vi{
    UIView *line = [[UIView alloc]initWithFrame:rect];
    line.backgroundColor = color;
    [vi addSubview:line];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
